﻿namespace CommonUtilityInfrastructure.Tests
{
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Linq;
    using System.Text;
    using CommonUtilityInfrastructure;
    using NUnit.Framework;
    using SoftwareApproach.TestingExtensions;
    using UsefulTools.Switches;
    using Switch = UsefulTools.Switches.Switch;

    [TestFixture]
    public class SwitchTests
    {
        enum State
        {
            Error,
            Inactive,
            Idle,
            Active
        }
        [Test]
        public void Switch_valued()
        {
            var state = State.Active;
            string message = Switch.Into<string>().From(state)
                .Case(State.Active, "Working.")
                .Case(State.Inactive, State.Idle, "Not working.")
                .Case(State.Error, "Broken.")
                .Default("No info");

            Assert.AreEqual("Working.", message);

        }
        [Test]
        public void Switch_standard()
        {
            bool workDone = false;
            var state = State.Active;
            Switch.On(state)
                .Case(State.Active, () => workDone = true)
                .CaseNop(State.Inactive, State.Idle)
                .Case(State.Error, () => ShowError("Bad."))
                .ThrowIfNoMatch();

            workDone.ShouldEqual(workDone);
        }

        private class A
        {
        }

        private class B : A
        {
        }

        private class C : A
        {
        }

        [Test]
        public void Switch_valued_fromtype()
        {
            A obj = new C();
            string result = Switch.Into<string>().FromTypeOf(obj)
                .Case<B>(a => "B")
                .Case<C>(a => "C")
                .GetResult();

            result.ShouldEqual("C");
        }

        private void ShowError(string ss)
        {
            
        }
      
        [Test]
        public void Switch_collective()
        {
            var strings = new[]
            {
                "A",
                "A",
                "C",
                "A",
                "A",
                "B",
                "A",
            };

            String str = Switch.Into<string>().AsCascadingCollectiveOf(strings)
                .CaseAny("A", "A")
                .CaseAll("C", "C")
                .GetValue();

            Assert.AreEqual("A", str);
        }

        [Test]
        public void CollectiveSwitchTest2()
        {
            var strings = new[]
            {
                "A",
                "A",
                "A",
                "A",
                "A",
                "A",
                "A",
            };

            String str = Switch.Into<string>().AsCascadingCollectiveOf(strings)
                .CaseAny("B", "B")
                .CaseAll("A", "A").GetValue();

            Assert.AreEqual("A", str);
        }
        [Test]
        public void CollectiveSwitchTest3()
        {
            var strings = new[] { "A","C", "A","C","B","A","A",};

            String result1 = Switch.Into<string>().AsCascadingCollectiveOf(strings)
                .CaseAny("B", "B") // This (first CaseAny) has the heighest priority.
                .CaseAny("C", "C") // This has a lower priority.
                .CaseAll("A", "A") // Only one CaseAll allowed. Lowest priority.
                .GetValue();

            Assert.AreEqual("B", result1);

            String result2 = Switch.Into<string>().AsCascadingCollectiveOf(strings)
                .CaseAny("D", "D") 
                .CaseAny("C", "C") 
                .CaseAll("B", "B") 
                .GetValue();

            Assert.AreEqual("C", result2);
        }
    }
}

